<?php
include_once 'Constant.php';
class CheckInput
{
	
	public static function check($inputArray)
	{
		if( isset($inputArray) == FALSE )
			return null;

		foreach ($inputArray as $key => $value)
		{
			$inputArray[$key] = trim($value);
		}
		
				
		//Check your name
		if(empty($inputArray['uName']) == TRUE)
		{
			$errArray["uNameErr"] = "Your name is required.";
		}
		else if( mb_strlen($inputArray['uName']) > Constant::MAX_LENGTH )
		{
			$errArray["uNameErr"] = "Your name must less than ". 
										 Constant::MAX_LENGTH." characters.";
		}

		// Check your email
		if(empty($inputArray['uEmail']) == TRUE)
		{
			$errArray["uEmailErr"] = "Your email is required.";
		}
		else if( mb_strlen($inputArray['uEmail']) > Constant::MAX_LENGTH )
		{
			$errArray["uEmailErr"] = "Your email must less than ". 
									Constant::MAX_LENGTH." characters.";				
		}
		else if(self::checkEmail($inputArray['uEmail']) == FALSE)
		{
			$errArray["uEmailErr"] = "Your email is not correct.";
		}
		
		//Check recipient's name
		if(empty($inputArray['rName']) == TRUE)
		{
			$errArray["rNameErr"] = "Recipient's name is required.";
		}
		else if( mb_strlen($inputArray['uName']) > Constant::MAX_LENGTH )
		{
			$errArray["rNameErr"] = "Recipient's name must less than ". 
									Constant::MAX_LENGTH." characters.";
		}

		if( empty($inputArray['chkEmail']) == FALSE )
		{
			// Check recipient's email
			if( empty($inputArray['rEmail']) == TRUE )
			{
				$errArray["rEmailErr"] = "Recipient's name is required.";
			}
			else if( mb_strlen($inputArray['rEmail']) > Constant::MAX_LENGTH )
			{
				$errArray["rEmailErr"] = "Recipient's email must less than ". 
										Constant::MAX_LENGTH." characters.";				
			}
			else if( self::checkEmail($inputArray['rEmail']) == FALSE)
			{
				$errArray["rEmailErr"] = "Recipient's email is not correct.";
			}
		}
		
		//Check recipient's message
		if(empty($inputArray['rMessage']) == TRUE)
		{
			$errArray["rMessageErr"] = "Recipient's message is required.";
		}
		else if( mb_strlen($inputArray['uName']) > Constant::MAX_LENGTH )
		{
			$errArray["rMessageErr"] = "Recipient's message must less than ". 
									Constant::MAX_LENGTH." characters.";
		}
		
		return $errArray;
	}

	private static function checkEmail($email)
	{
		return preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",
		$email) ? TRUE : FALSE;
	}
}

?>